Linux:10 个 Netstat 命令示例

您所在的位置:网站首页 netstat -ntlp怎么读 Linux:10 个 Netstat 命令示例

Linux:10 个 Netstat 命令示例

2024-07-17 19:20| 来源: 网络整理| 查看: 265

Netstat 命令显示各种网络相关信息,例如网络连接、路由表、接口统计信息、伪装连接、多播成员资格等,

在本文中,让我们了解 10 个实用的 unix netstat 命令示例。

1.列出所有端口(监听和非监听端口) 使用 netstat -a 列出所有端口 # netstat -a | more Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN udp 0 0 *:bootpc *:* Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 6135 /tmp/.X11-unix/X0 unix 2 [ ACC ] STREAM LISTENING 5140 /var/run/acpid.socket 使用 netstat -at 列出所有 tcp 端口 # netstat -at Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN 使用 netstat -au 列出所有 udp 端口 # netstat -au Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:bootpc *:* udp 0 0 *:49119 *:* udp 0 0 *:mdns *:* 2.列出处于侦听状态的套接字 使用 netstat -l 仅列出监听端口 # netstat -l Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:ipp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN udp 0 0 *:49119 *:* 使用 netstat -lt 仅列出侦听 TCP 端口 # netstat -lt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN 使用 netstat -lu 仅列出侦听的 UDP 端口 # netstat -lu Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:49119 *:* udp 0 0 *:mdns *:* 使用 netstat -lx 仅列出监听的 UNIX 端口 # netstat -lx Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 6294 private/maildrop unix 2 [ ACC ] STREAM LISTENING 6203 public/cleanup unix 2 [ ACC ] STREAM LISTENING 6302 private/ifmail unix 2 [ ACC ] STREAM LISTENING 6306 private/bsmtp 3. 显示每个协议的统计数据 使用 netstat -s 显示所有端口的统计信息 # netstat -s Ip: 11150 total packets received 1 with invalid addresses 0 forwarded 0 incoming packets discarded 11149 incoming packets delivered 11635 requests sent out Icmp: 0 ICMP messages received 0 input ICMP message failed. Tcp: 582 active connections openings 2 failed connection attempts 25 connection resets received Udp: 1183 packets received 4 packets to unknown port received. ..... 使用 netstat -st(或)-su 显示 TCP(或)UDP 端口的统计信息 # netstat -st # netstat -su 4. 使用 netstat -p 在 netstat 输出中显示 PID 和程序名称

netstat -p 选项可以与任何其他 netstat 选项结合使用。这会将“PID/程序名称”添加到 netstat 输出。这在调试以确定哪个程序在特定端口上运行时非常有用。

# netstat -pt Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 1 0 ramesh-laptop.loc:47212 222.168.185.75:www CLOSE_WAIT 2109/firefox tcp 0 0 ramesh-laptop.loc:52750 lax:www ESTABLISHED 2109/firefox 5.不要在netstat输出中解析主机、端口和用户名

当您不想显示主机、端口或用户的名称时,请使用 netstat -n 选项。这将以数字显示,而不是解析主机名、端口名、用户名。

这也加快了输出速度,因为 netstat 不执行任何查找。

# netstat -an

如果您不想只解析这三个项目(端口、主机或用户)中的任何一项,请使用以下命令。

# netsat -a --numeric-ports # netsat -a --numeric-hosts # netsat -a --numeric-users 6.连续打印netstat信息

netstat 将每隔几秒钟连续打印一次信息。

# netstat -c Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 ramesh-laptop.loc:36130 101-101-181-225.ama:www ESTABLISHED tcp 1 1 ramesh-laptop.loc:52564 101.11.169.230:www CLOSING tcp 0 0 ramesh-laptop.loc:43758 server-101-101-43-2:www ESTABLISHED tcp 1 1 ramesh-laptop.loc:42367 101.101.34.101:www CLOSING ^C 7. 在你的系统中找到不支持的地址族 netstat --verbose

最后,你会得到这样的东西。

netstat: no support for `AF IPX' on this system. netstat: no support for `AF AX25' on this system. netstat: no support for `AF X25' on this system. netstat: no support for `AF NETROM' on this system. 8.使用netstat -r显示内核路由信息 # netstat -r Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.1.0 * 255.255.255.0 U 0 0 0 eth2 link-local * 255.255.0.0 U 0 0 0 eth2 default 192.168.1.1 0.0.0.0 UG 0 0 0 eth2

注意:使用 netstat -rn 以数字格式显示路由而不解析主机名。

9.找出程序在哪个端口上运行 # netstat -ap | grep ssh (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 1 0 dev-db:ssh 221.174.100.22:39213 CLOSE_WAIT - tcp 1 0 dev-db:ssh 221.174.100.22:57643 CLOSE_WAIT -

找出哪个进程正在使用特定端口:

# netstat -an | grep ':80' 10.显示网络接口列表 # netstat -i Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 0 0 0 0 0 0 0 0 BMU eth2 1500 0 26196 0 0 0 26883 6 0 0 BMRU lo 16436 0 4 0 0 0 4 0 0 0 LRU

使用 netstat -ie 显示接口的扩展信息(类似于 ifconfig):

# netstat -ie Kernel Interface table eth0 Link encap:Ethernet HWaddr 00:10:40:22:11:11 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) Memory:f6ae0000-f6b00000


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3